package com.ruoyi.system.timing.callback.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.QQSMSUtils;
import com.ruoyi.system.domain.AwMyPlan;
import com.ruoyi.system.domain.AwPlanDetail;
import com.ruoyi.system.enums.DetailStatusEnum;
import com.ruoyi.system.enums.PlanStatusEnum;
import com.ruoyi.system.service.IAwMyPlanService;
import com.ruoyi.system.service.IAwPlanDetailService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.timing.callback.TimingCallback;
import com.ruoyi.system.timing.entity.PoolDomain;
import com.ruoyi.system.xmlAbstract.XmlGenerateAbstract;
import com.ruoyi.system.xmlAbstract.context.XmlBuildContext;
import com.ruoyi.system.xmlAbstract.interfaces.analysisInterfaces.impl.H5XmlAnalysis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author : anxinr
 * @date : 2023/8/17 17:34
 */
@Slf4j
@Component
public class SchedulePlanCallback implements TimingCallback {
    @Autowired
    private IAwPlanDetailService detailService ;
    @Autowired
    private IAwMyPlanService planService ;
    @Autowired
    private ISysUserService userService ;

    @Override
    public void callBack(PoolDomain data){
        try {
            AwPlanDetail detail = (AwPlanDetail) data.getData() ;
            LambdaQueryWrapper<AwPlanDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AwPlanDetail::getPlanId ,detail.getPlanId()) ;
            List<AwPlanDetail> details = detailService.list(wrapper);
            AwMyPlan plan = planService.getById(detail.getPlanId());

            XmlBuildContext xmlBuildContext = XmlBuildContext.getInstance();
            xmlBuildContext.initXmlData("QQEmailH5XmlTemplate");
            xmlBuildContext.setAnalysis(new H5XmlAnalysis());
            XmlGenerateAbstract xmlBuild = xmlBuildContext.getXmlBuild();

            xmlBuild.generateChildNode("caption" , plan.getName());
            xmlBuild.generateChildNode("tr");
            xmlBuild.setCurrentNodeToChildNodeByNodeName("tr");
            xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" ,"计划名称");
            xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" ,"开始时间");
            xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" ,"结束时间");
            xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" ,"是否完成");
            xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" ,"操作");
            xmlBuild.changeCurrentNodeToParentNode();

            for (int i = 0; i < details.size(); i++) {
                xmlBuild.generateChildNode("tr");
                xmlBuild.setCurrentNodeToChildNodeByNodeName("tr" ,i + 1);
                xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" ,details.get(i).getScheduleDescribe());
                xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" ,"开始时间为" + details.get(i).getStartTime());
                xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" ,"结束时间为" + details.get(i).getEndTime());
                xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" , PlanStatusEnum.PLAN_STATUS_NORMAL.getCode().equals(details.get(i).getStatus()) ? "未完成" : "已完成");
                xmlBuild.generateChildNode("td"," align=\"center\" bgcolor=\"#808080\"" ,
                        "<a href='http://" +
                                "120.48.125.48" +
                                ":8080/system/detail/complete?id=" +
                                details.get(i).getId().toString() +
                                "'"+
                                ">" +
                                (details.get(i).getStatus().equals(DetailStatusEnum.DETAIL_STATUS_NORMAL.getCode()) ? "完成" : "") +
                                "</a>"
                        );
                xmlBuild.changeCurrentNodeToParentNode();
            }
            xmlBuildContext.analysisXmlTemplate();
            SysUser user = userService.selectUserById(plan.getUserId());
            QQSMSUtils.noticeTalentEmail(user.getEmail() ,plan.getName() ,xmlBuildContext.getXmlBuild().xml.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
